package com.scau.hwadee.examination.controller;

import com.scau.hwadee.examination.entity.Examdate;
import com.scau.hwadee.examination.entity.Examsession;
import com.scau.hwadee.examination.entity.Unifiedexamschedule;
import com.scau.hwadee.examination.service.ExaminationManagementService;
import com.scau.hwadee.examination.utils.ImportExcelUtils;
import com.scau.hwadee.examination.utils.JsonUtils;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author YSY
 * @date 2019/8/1 11:32
 * description
 */
@Slf4j
@Controller
@RequestMapping("examination/management")
@Api(tags = "ExaminationManagementController", description = "考次管理模块")

public class ExaminationManagementController {
  @Autowired
  private ExaminationManagementService examinationManagementService;

  @PostMapping("importExaminationExcel")
  @ApiOperation("导入统考Excel表")
  public ResponseEntity<ResponseVo> importExaminationExcel(MultipartFile file) {
    log.debug("导入统考Excel表");
    ResponseVo vo = ResponseVo.instance();
    try {
      List<Unifiedexamschedule> unifiedExamSchedule = ImportExcelUtils.parseExaminationExcel(file.getInputStream());
      int cnt = examinationManagementService.insertunifiedExamScheduleByExcel(unifiedExamSchedule);
      String message;
      if (cnt > 0) {
        message = "导入Excel表成功";
      } else {
        message = "导入Excel表失败";
      }
      vo.setMessage(message);
    } catch (Exception e) {
      vo.setMessage("导入Excel表失败");
    }
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("NewExamsession")
  @ApiOperation("增加新的考次")
  public ResponseEntity<ResponseVo> createNewExamsession(@RequestBody Examsession examsession) {
    log.debug("增加新的考次");
    log.debug("考次信息为:"+ JsonUtils.ObjectToJSON(examsession));
    ResponseVo vo = ResponseVo.instance();
    int cnt = examinationManagementService.insertNewExamsession(examsession);
    if (cnt > 0) {
      vo.setMessage("增加考次成功");
    } else {
      vo.setMessage("增加考次失败");
    }
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allExamsession")
  @ApiOperation(value = "获取所有考次信息")
  public ResponseEntity<ResponseVo> getAllExamsession(@RequestParam("pageNum") int pageNum,
                                                      @RequestParam("pageSize") int pageSize,
                                                      @RequestParam(value = "state", required = false) Integer state) {
    log.debug("获取所有考次信息");
    ResponseVo vo = ResponseVo.instance();
    vo.setData(examinationManagementService.getAllExamSessions(pageNum, pageSize, state));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allMV_ID")
  @ApiOperation(value = "获取所有MV_ID")
  public ResponseEntity<ResponseVo> getAllMajorPlanVersion(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
    log.debug("获取所有MV_ID");
    ResponseVo vo = ResponseVo.instance();
    vo.setData(examinationManagementService.getAllMajorPlanVersion(pageNum, pageSize));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("edTimecode")
  @ApiOperation(value = "根据考次编码获取时间编码")
  public ResponseEntity<ResponseVo> getedTimecodeByesId(@RequestParam("esId") String esId) {
    ResponseVo vo = ResponseVo.instance();
    vo.setData(examinationManagementService.getByesIdForedTimecode(esId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("ExamDate")
  @ApiOperation(value = "根据主键获取考试日期")
  public ResponseEntity<ResponseVo> getExamDateByPrimaryKey(@RequestParam("esId") String esId) {
    ResponseVo vo = ResponseVo.instance();
    vo.setData(examinationManagementService.getExamDate(esId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("NewExamDate")
  @ApiOperation("更新考试日期")
  public ResponseEntity<ResponseVo> updateNewExamDate(@RequestBody Examdate record) {
    log.debug("更新考试日期");
    ResponseVo vo = ResponseVo.instance();
    int cnt = examinationManagementService.updateNewExamdate(record);
    if (cnt > 0) {
      vo.setMessage("更新考试日期成功");
    } else {
      vo.setMessage("更新考试日期失败");
    }
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }
}
